package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuDao menuDao;

    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    @Override
    public Menu editById(Integer id) {
        return menuDao.editById(id);
    }

    //判断菜单是否被角色关联
    @Override
    public void deleteById(Integer id) {
        long count = menuDao.findRelationById(id);
        if (count>0){
            //说明被关联，不能删除
            throw new BusinessRuntimeException("该菜单被角色表关联，不能删除");
        }else {
            menuDao.deleteById(id);
        }
    }

    @Override
    public PageResult findMenuPage(QueryPageBean queryPageBean) {
        log.debug("service:" + queryPageBean);
        //1. 开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2. 条件查询
        Page<Menu> page = menuDao.findByCondition(queryPageBean.getQueryString());
        log.debug("page{"+page+"}");
        //封装PageResult
        return new PageResult(page.getTotal(), page);
    }
}
